//
//  InviteListViewController.m
//  CBT
//
//  Created by Mr.Zhang on 2019/1/11.
//  Copyright © 2019 Mr.Zhang. All rights reserved.
//

#import "InviteListViewController.h"
#import "InvitationRevenueViewController.h"

#import "InviteListModel.h"
#import "UserManager.h"
#import "InviteListCell.h"
#import "InviteListNewCell.h"
#import "InviteListHeaderView.h"

@interface InviteListViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) InviteListHeaderView *headerView;
@property (nonatomic, strong) InviteListModel *model;

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, copy  ) NSString *page;

@end

@implementation InviteListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.page = @"1";
    
    [self.view addSubview:self.tableView];
    
    self.view.backgroundColor = UIColorFromRGB(0xF7F7F7);
    self.navigationItem.title = kLocalizedString(@"我的邀请", nil);
}

#pragma mark - OutPut
- (void)getInviteListWithPage:(NSString *)page {
    @weakify(self);
    [UserManager getRewardRecordWithUserId:[CBTLocalUserManager sharedInstance].userId page:self.page block:^(id data, HttpRequestResult result, NSString *msg) {
        self_weak_.headerView.hidden = NO;
        [self_weak_ stopLoading];
        if (result == kHttpRequestResultNormal) {
            RewardRecordModel *model = data;
            self_weak_.headerView.model = model;
            if ([self_weak_.page intValue] == 1) {
                self_weak_.dataArray = [NSMutableArray array];
            }
            
            if (model.rewardRecord.count == 0 && [NSArray isArrayEmpty:self_weak_.dataArray]) {
                self_weak_.tableView.mj_footer.hidden = YES;
            }
            else {
                self_weak_.tableView.mj_footer.hidden = NO;
                [self_weak_.dataArray addObjectsFromArray:model.rewardRecord];
                self_weak_.page = [NSString stringWithFormat:@"%d",[self_weak_.page intValue] + 1];
            }
            if ([self_weak_.page intValue] > [model.totalPage intValue]) {
                [self_weak_.tableView.mj_header endRefreshing];
                [self_weak_.tableView.mj_footer endRefreshingWithNoMoreData];
            }
            else {
                [self_weak_.tableView.mj_header endRefreshing];
                [self_weak_.tableView.mj_footer endRefreshing];
            }
        }
        else {
            [HFStatusView showFailureMessage:msg];
            if (self_weak_.dataArray.count == 0) {
                self_weak_.tableView.mj_footer.hidden = YES;
            }
            [self_weak_.tableView.mj_header endRefreshing];
            [self_weak_.tableView.mj_footer endRefreshing];
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self_weak_.tableView reloadData];
        });
    }];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    @weakify(self);
    
    InviteListNewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
    cell.cellType = 0;
    [cell setInviteListCellBlock:^(NSInteger type, NSString *investFlag, NSString *userId) {
//        if (type == 0 /*&& [investFlag intValue] == 0*/) {return;}
        InvitationRevenueViewController *vc = [[InvitationRevenueViewController alloc]init];
        vc.showType = type == 0 ? 1 : 2;
        vc.userId = userId;
        vc.navigationItem.title = kLocalizedString(type == 0 ? @"好友投资详情" : @"好友邀请列表", nil);
        [self_weak_.navigationController pushViewController:vc animated:YES];
    }];
    cell.rewardRecordModel = self.dataArray[indexPath.row];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 79;
}

#pragma mark - Lazy
- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (InviteListHeaderView *)headerView {
    
    if (!_headerView) {
        _headerView = [[[NSBundle mainBundle] loadNibNamed:@"InviteListHeaderView" owner:nil options:nil]objectAtIndex:0];
        _headerView.frame = CGRectMake(0, 0, SCREEN_WIDTH, 165);
    }
    return _headerView;
}

- (UITableView *)tableView
{
    if (!_tableView) {
        
         @weakify(self);
        
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, ScreenNarrowSide, ScreenWideSide - self.tabBarController.tabBar.bounds.size.height - (HFIphoneX ? HFIPhoneXSafeAreaPotraitBottom : 0) - 68 - HFNavigationHeight) style:(UITableViewStylePlain)];
        _tableView.backgroundColor = UIColorFromRGB(0xF7F7F7);
        _tableView.showsVerticalScrollIndicator = NO;
        
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.showNoDataEmptyView = YES;
        
        _tableView.delegate   = self;
        _tableView.dataSource = self;
        
        [_tableView setReloadBtnAction:^{
            [self_weak_ startLoading];
            self_weak_.page = @"1";
            [self_weak_ getInviteListWithPage:self_weak_.page];
        }];
        
        if (@available(iOS 11.0, *)){
            _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        }
        else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        
        _tableView.estimatedRowHeight           = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
        _tableView.estimatedSectionFooterHeight = 0;
        
        [_tableView registerNib:[UINib nibWithNibName:@"InviteListNewCell" bundle:nil] forCellReuseIdentifier:@"Cell"];
        
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self_weak_.page = @"1";
            [self_weak_ getInviteListWithPage:self_weak_.page];
        }];
        
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
            [self_weak_ getInviteListWithPage:self_weak_.page];
        }];
        
        _tableView.tableHeaderView.hidden = YES;

        _tableView.tableHeaderView  = self.headerView;
        _tableView.tableFooterView  = [UIView new];
        _tableView.mj_footer.hidden = YES;
        
        [_tableView.mj_header beginRefreshing];
    }
    return _tableView;
}

@end
